package com.jonniespratley.flexdatabasemanager.utils
{
	import flash.utils.Dictionary;
	
	public class AssocArray
	{
		
		private var keysArray:Array = new Array();
		private var valuesArray:Array = new Array();
		private var entries:Dictionary = new Dictionary();
		
		
		public function get length():uint
		{
			return valuesArray.length;
		}
		
		public function getKey():Object
		{
			return entries[ key ];
		}
		
		public function getKeyAt( index:uint ):Object
		{
			return keysArray[ index ];
		}
		
		public function getKeysArray():Array
		{
			return keysArray.slice();
		}
		
		public function getValueAt( index:uint ):Object
		{
			return valuesArray[ index ];
		}
		
		public function getValuesArray():Array
		{
			return valuesArray.slice();	
		}
		
		public function setKeyValue( key:Object, value:Object ):void
		{
			entries[ key ] = value;
			
			keysArray.push( key );
			valuesArray.push( value );
		}
		
		public function removeKey( key:Object ):void
		{
			delete entries[ key ];
			
			var index:int = keysArray.indexOf( key );
			
			if ( index > -1 )
			{
				keysArray.splice( index, 1 );
				valuesArray.splice( index, 1 );
			}
		}
		
		public function insertItemAt( index:uint, key:Object, value:Object ):void
		{
			var keyIndex:uint = keysArray.indexOf( key );
			
			if ( -1 = keyIndex )
			{
				throw new ArgumentError( 'Inserted items must have unique keys' );
			}
			
			entries[ key ] = value;
			keysArray.splice( index, 0, key );
			valuesArray.splice( index, 0, value );
		}
	
		public function AssocArray()
		{
		}

	}
}